package com.cn.por.util.sqlinsert;

import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateUtil;

import java.time.LocalDate;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * sql参数填充,将mybatis log输出的sql脚本和参数整合为完整的可以直接执行的sql语句
 */
public class SqlParamterInsert {


    public static void main(String[] args) {
        /**
         * paramter 参数
         * sql 原始sql脚本
         */
        insertParamter(
                " fsm(String), 过度医疗(String), H43112100148(String), 431121(String), 2024-01-01(String), 2024-11-21(String), 431121(String), 2024-01-01(String), 2024-11-21(String), 310(String), H43112100148(String)",
                "elect * from(select round(ifnull(sum(t1.VOLA_AMT),0),2) warn_Amt, round(ifnull(sum(t1.VOLA_NUM),0),2) warn_Cnt from FSSP_ANA_RSLT_D t1,fssp_rule_type_scd_b t2 where t1.RULE_TYPE = t2.CLASS_CODE and t1.SCR_FLAG = '1' and t1.VALI_FLAG = '1' and t1.DEL_FLAG = '0' and t1.MODL_TYPE = ? and t2.SCREEN_TYPE = ? and t1.FIXMEDINS_CODE = ? and t1.FIX_BLNG_ADMDVS like concat(?, '%') and t1.BIZ_BEGNTIME >= ? and t1.BIZ_ENDTIME <= ?) t1, (select round(ifnull(sum(t1.MEDFEE_SUMAMT),0),2) medfee_Amt, round(ifnull(sum(t1.HIFP_PAY),0),2) fund_amt, case when ifnull(sum(MEDFEE_SUMAMT),0) = 0 then 0 else round(ifnull(sum(t1.HIFP_PAY)/sum(MEDFEE_SUMAMT),0),4)*100 end fund_Ratio from fssp_fixmedins_hifp_pay_a t1 where t1.ADMDVS_CODE like concat(?,'%') and t1.del_flag = '0' and t1.VALI_FLAG = '1' and t1.USE_DATE >= substr(?,1,7) and t1.USE_DATE <= substr(?,1,7) and t1.insu_type = ? and t1.FIXMEDINS_CODE = ?) t2 ");

    }

    public static String insertParamter(String paramter,String sql){
        // 将表示日期的字符串转换为LocalDate对象（这里示例中直接写死日期，实际可按需替换）
        LocalDate localDate = LocalDate.of(2024, 11, 22);

        // 将LocalDate对象转换为ZonedDateTime对象，设置时区（这里使用系统默认时区）
        ZonedDateTime zonedDateTime = localDate.atStartOfDay(ZoneId.systemDefault());

        // 获取从1970-01-01T00:00:00Z到指定日期时间对应的毫秒数
        long milliseconds = zonedDateTime.toInstant().toEpochMilli();

        System.out.println("2024-01-01对应的毫秒数为：" + milliseconds);
        /**
         * 将参数转换为数组
         */
        String[] split =paramter.replace(" ","").split(",");
        StringBuffer resultSql =new StringBuffer(sql);
        for (int i = 0; i < split.length; i++) {
            resultSql= new StringBuffer(resultSql.toString().replaceFirst("\\?",
                    split[i].indexOf("(String)")>=0 ? "'"+split[i].replace("(String)","")+"'"
                            :split[i].replace("(int)","")));
        }

        System.out.println(resultSql);
        return resultSql.toString();
    }




}
